package com.yonyou.iuap.activiti.demo.test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * @author guoxh
 * @DATE 2019/6/30 19:35
 * @DESC com.yonyou.iuap.activiti.demo.test
 * act_get_bytearray 存放流程定义信息
 * act_ru_task
 * act_re_procdef 流程定义
 *
 * https://blog.csdn.net/cs_hnu_scw/article/details/79059965
 **/
public class ActivitiTable {

    @Test
    public void createTable(){
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
                .buildProcessEngine();
    }

    /**
     * 1. 部署一个流程
     * 2. 启动流程实例
     * 3. 请假人发出请假申请
     * 4. 班主任查看任务
     * 5. 班主任审批
     * 6. 教务处审批
     */
    /**
     * 运行一个Activiti
     */
    @Test
    public void createActivitiTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("test.bpmn")
                .addClasspathResource("test.png")
                .deploy();
    }

    /**
     * 启动一个流程示例
     */
    @Test
    public void testStartProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //"shenqing:1:4" 从 act_re_procdef 表中获取
        processEngine.getRuntimeService().startProcessInstanceById("shenqing:1:4");
        System.out.println("流程启动成功");
    }

    /**
     * 完成请假申请
     */
    @Test
    public void testQingjia(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //"2504" 从act_ru_task 表中获取
        processEngine.getTaskService().complete("2504");
    }

    @Test
    public void testQueryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //此处的 小毛 就是我们在之前设计的流程图中添加的班主任内容
        List<Task> taskList = processEngine.getTaskService().createTaskQuery()
                .taskAssignee("小毛")
                .list();
        for(Task task : taskList){
            System.out.println(task.getName());
        }
    }

    /**
     * 班主任小毛完成任务
     */
    @Test
    public void testFinishTask_manager(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //查看 act_ru_task 数据表
        processEngine.getTaskService().complete("5002");
    }

    /**
     * 教务处大毛完成任务
     */
    @Test
    public void testFinishTask_boss(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //查看 act_ru_task 数据表
        processEngine.getTaskService().complete("7502");
    }
}
